<!-- 角色选择 按钮 -->
<template>
  <div id="dialog-container">
    <el-button type="primary" plain @click="dialogVisible = true">选择</el-button>
    <el-dialog v-dialogDrag title="角色选择" append-to-body :visible.sync="dialogVisible" width="580px" :close-on-click-modal="false">
      <el-table :data="tableData" size="small" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="50"></el-table-column>
        <el-table-column type="index" align="center" label="序号" width="50"></el-table-column>
        <el-table-column prop="name" align="center" label="角色" min-width="100"></el-table-column>
        <el-table-column prop="date" align="center" label="用户姓名" min-width="100"></el-table-column>
        <el-table-column prop="id" align="center" label="账号" min-width="100"></el-table-column>
      </el-table>
      <el-row type="flex" justify="center">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="pageSizeArr"
          :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pageTotal"></el-pagination>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="choose">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { getAllRoleList } from 'service/task';
  export default {
    data() {
      return {
        dialogVisible: false,
        currentPage: 1,
        pageSizeArr: [10, 20, 50, 100],
        pageSize: 10,
        pageTotal: 10,
        tableData: [{
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        }, {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄'
        }, {
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄'
        }, {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄'
        }],
        selectionVal: ''
      }
    },
    mounted() {
      this.showList();
    },
    methods: {
      handleSelectionChange(val) {
        // for(var i = 0; i < val.length; i++) {
        //   selectionVal.push(val[i].username);
        // }
        this.selectionVal = val[0].name;
      },
      handleSizeChange(val) {
        this.pageSize = val;
        this.showList();
      },
      handleCurrentChange(val) {
        this.pageNumber = val;
        this.showList();
      },
      showList() {
        this.loading = true;
        getAllRoleList()
          .then(res => {
            this.tableData = res.data.result.content;
            this.currentPage = res.data.result.pageable.pageNumber;
            this.pageSize = res.data.result.pageable.pageSize;
            this.pageTotal = res.data.result.totalPages;
            this.loading = false;
          })
      },
      choose() {
        this.$emit('role', this.selectionVal);
        this.dialogVisible = false;
      }
    }
  }
</script>
